#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "common.h"
#include "database.h"

#include <QDebug>
#include <QDateTime>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    setWindowTitle("Sqlite3 Demo");

    qInfo() << "[MAIN] Application Version" << QApplication::applicationVersion();

    initDatabase();

    initSubThread();
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::recvCurrentID_slot(int curID)
{
    ui->label_curID->setText(QString::number(curID));
}

void MainWindow::recvProgress_slot(int all, int valid, int upload)
{
    ui->label_all->setText(QString::number(all));
    ui->label_valid->setText(QString::number(valid));
    ui->label_upload->setText(QString::number(upload));
}

void MainWindow::on_pBtn_change_clicked()
{
    QDate m_date = ui->dateEdit->date();

    QString dateString = m_date.toString("yyyy_MM_dd");

    std::string date = dateString.toStdString();

    m_db_path.SetDRPath(date);
}

void MainWindow::initDatabase()
{
    DataBase database;
    database.drDataCreate();
}

void MainWindow::initSubThread()
{
    initInsertThread();

    initSelectThread();

    initOtherThread();
}

void MainWindow::initInsertThread()
{
    insertObj    = new InsertThread;
    insertThread = new QThread();
    insertObj->moveToThread(insertThread);
    connect(insertThread, &QThread::started, insertObj, &InsertThread::startThread_slot);
    connect(this, &MainWindow::destroyed, insertObj, &InsertThread::exitThread_slot);
    connect(this, &MainWindow::destroyed, this, [=]() {
        insertThread->quit();
        insertThread->wait();
    });
    connect(insertThread, &QThread::finished, insertObj, &InsertThread::deleteLater);
    connect(insertThread, &QThread::finished, insertThread, &QThread::deleteLater);
    insertThread->start();
}

void MainWindow::initSelectThread()
{
    selectObj    = new SelectThread;
    selectThread = new QThread();
    selectObj->moveToThread(selectThread);
    connect(selectThread, &QThread::started, selectObj, &SelectThread::startThread_slot);
    connect(selectObj, &SelectThread::sendCurrentID_signal, this, &MainWindow::recvCurrentID_slot);
    connect(this, &MainWindow::destroyed, selectObj, &SelectThread::exitThread_slot);
    connect(this, &MainWindow::destroyed, this, [=]() {
        selectThread->quit();
        selectThread->wait();
    });
    connect(selectThread, &QThread::finished, selectObj, &SelectThread::deleteLater);
    connect(selectThread, &QThread::finished, selectThread, &QThread::deleteLater);
    selectThread->start();
}

void MainWindow::initOtherThread()
{
    otherObj    = new OtherThread;
    otherThread = new QThread();
    otherObj->moveToThread(otherThread);
    connect(otherThread, &QThread::started, otherObj, &OtherThread::startThread_slot);
    connect(otherObj, &OtherThread::sendProgress_signal, this, &MainWindow::recvProgress_slot);
    connect(this, &MainWindow::destroyed, otherObj, &OtherThread::exitThread_slot);
    connect(this, &MainWindow::destroyed, this, [=]() {
        otherThread->quit();
        otherThread->wait();
    });
    connect(otherThread, &QThread::finished, otherObj, &OtherThread::deleteLater);
    connect(otherThread, &QThread::finished, otherThread, &QThread::deleteLater);
    otherThread->start();
}
